package com.gla.giliair.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/23 15:00
 */
public class UploadUtil {
    private static final String writePath = "G:\\PP\\local\\file";

    public static Map<String, Object> uploadFile(MultipartFile file) {
        // 判断所上传文件是否存在
        if (file.isEmpty()) {
            return MapGenerator.getRes(521, "文件上传不能为空！");
        }

        //获取文件保存路径
        //String path = request.getServletContext().getRealPath("/uploads");
        File filepath = new File(writePath);
        //判断文件目录是否存在，若不存在创建目录
        if (!filepath.exists()) {
            filepath.mkdirs();
        }

        //获取文件原始文件名
        String oldName = file.getOriginalFilename();
        //使用UUID构造新的文件名防止文件名冲突
        String newName = UUID.randomUUID() + "_" + oldName;
        //设置文件最终保存位置及对应文件名
        File newFile = new File(filepath + File.separator + newName);

        try {
            //上传并保存文件到指定位置
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
            return MapGenerator.getRes(522, "文件上传失败！");
        }

        return MapGenerator.getResWithData(200, "文件上传成功！", "/file/" + newName);
    }

    public static Map<String, Object> uploadFiles(List<MultipartFile> files) {
        // 判断所上传文件是否存在
        if (files.size() == 1 && files.get(0).isEmpty()) {
            return MapGenerator.getRes(521, "多文件上传不能为空，至少一个！");
        }

        //获取文件保存路径
        //String path = request.getServletContext().getRealPath("/uploads");
        File filepath = new File(writePath);
        //判断文件目录是否存在，若不存在创建目录
        if (!filepath.exists()) {
            filepath.mkdirs();
        }

        List<String> urlList = new ArrayList<String>();

        //循环输出上传的文件
        for (MultipartFile file : files) {
            //获取文件原始文件名
            String oldName = file.getOriginalFilename();
            //使用UUID构造新的文件名防止文件名冲突
            String newName = UUID.randomUUID() + "_" + oldName;
            //设置文件最终保存位置及对应文件名
            File newFile = new File(filepath + File.separator + newName);

            try {
                //上传并保存文件到指定位置
                file.transferTo(newFile);
                urlList.add("/file/" + newName);
            } catch (IOException e) {
                e.printStackTrace();
                return MapGenerator.getRes(522, "多文件上传失败！");
            }
        }

        return MapGenerator.getResWithData(200, "多文件上传成功！", urlList);
    }

    public static String listToString(List<String> fileUrlsList) {
        String res = "";
        for (String url : fileUrlsList) {
            res = res + url + ",";
        }
        res = res.substring(0, res.length() - 1);
        return res;
    }

    public static List<String> stringToList(String fileUrls) {
        String[] pics = fileUrls.split(",");
        List<String> picList = new ArrayList<>(Arrays.asList(pics));
        return picList;
    }
}
